﻿#include "IRobot.h"
#include <QDebug>
#include "UDeviceConfig.h"
#include <qvector.h>
#include "UAddRob.h"
// 创建设备实例
DEVICE_EXPORT IDevice* createDevice(SDeviceConfig* pDevice)
{
	CPublicData::instance().init();
	return new IRobot(pDevice);  // 创建并返回一个 Robot 实例
}

DEVICE_EXPORT QVector<IDevice*>* createDevices(SDeviceConfig* pDevice)
{
	CPublicData::instance().init();
	UAddRob* dlg = new UAddRob(pDevice->sDeviceName, pDevice->sDeviceId);
	dlg->setWindowModality(Qt::ApplicationModal); // 应用程序模态
	dlg->show();
	QEventLoop loop;
	QObject::connect(dlg, &UAddRob::aboutToClose, &loop, &QEventLoop::quit);
	loop.exec();  // 阻塞，直到 w 被关闭
	QVector<IDevice*>* vecDevice = new QVector<IDevice*>();
	vecDevice->append(static_cast<IDevice*>(dlg->m_pIRobot));  // 向上转换
	return vecDevice;
}

// 销毁设备实例
DEVICE_EXPORT void destroyDevice(IDevice* dev) 
{
	if (dev) 
	{
		delete dev;  // 删除设备实例
	}
}

DEVICE_EXPORT QVector<QString>* getModelList()
{
	static QVector<QString> modelList = {"IRB-UT01"};
	return &modelList;
}



DEVICE_EXPORT QVector<QWidget*>* getPublicCardList()
{
	QVector<QWidget*>* vecWidget = new QVector<QWidget*>();
	return vecWidget;
}
DEVICE_EXPORT QWidget* getPublicCard(int nCardId, QString* psCardGuid)
{
	QWidget* pWidget = new QWidget();
	return pWidget;
}

// 构造函数
IRobot::IRobot()
{

}

IRobot::IRobot(Robot* pRobot, SDeviceConfig* pDeviceConfig)
{
	m_pRobot = pRobot;
	m_pDeviceConfig = pDeviceConfig;
}


IRobot::IRobot(SDeviceConfig * pDeviceConfig)
{
	m_pDeviceConfig = pDeviceConfig;
	m_pRobot = new Robot(pDeviceConfig->sDeviceName, pDeviceConfig->sDeviceId, pDeviceConfig->sConfig);
	m_pConfoig = nullptr;
}

// 析构函数
IRobot::~IRobot() 
{
	if (m_pRobot)
	{
		delete m_pRobot;
		m_pRobot = nullptr;
	}
}

QObject* IRobot::getDeviceObject()
{
	// 返回设备的底层对象
	return nullptr;
}

bool IRobot::startDevice()
{
	return m_pRobot->startDevice();
}

void IRobot::stopDevice()
{
	m_pRobot->stopDevice();
}

bool IRobot::controlDevice(const QString& cmd)
{
	// 控制设备的命令逻辑
	qDebug() << "发送设备命令: " << cmd;
	return true;
}

void IRobot::deleteDevice()
{
	m_pRobot->deleteDevice();
}

QString IRobot::getDeviceStatus() const
{
	// 获取设备的状态
	return IRobotStatus;
}

QList<CMeasurePoint*> IRobot::getMeasurePoints() const
{
	// 获取设备支持的测点
	return m_pRobot->getMeasurePoints();
}

void IRobot::setAlarmArea(const QString& area)
{
	// 设置设备报警区域
	alarmArea = area;
}

QList<QString> IRobot::getDeviceLogs() const
{
	// 获取设备日志
	return logs;
}

QWidget* IRobot::getDeviceConfigUi()
{
	//if (m_pConfoig != nullptr)
	//{
	//	delete m_pConfoig;
	//	m_pConfoig = nullptr;
	//}
	// 返回设备配置界面
	return m_pRobot->getDeviceConfigUi();
	
}

QWidget* IRobot::getDeviceRunUi()
{
	// 返回设备运行界面
	return m_pRobot->getMainUi();
}

QWidget* IRobot::getDeviceThumbnailUi(int nId)
{
	// 返回设备缩略图界面
	return m_pRobot->getPanel(nId);
}

QVector<QWidget*> IRobot::getDeviceUiList()
{
	return m_pRobot->getPanelList();
}

QMap<QString, QString> IRobot::getInputPointList()
{
	QMap<QString, QString>mapList;
	mapList.insert("a", "Root001.Node010.Dev003.DevState");
	mapList.insert("b", "Root001.Node010.Dev003.RobBatteryInfo");
	return mapList;
}


void IRobot::setInputPointList(QMap<QString, CMeasurePoint*>vecInputPoint)
{	
	/*vecInputPoint["a"]->setValue(1000);
	vecInputPoint["b"]->setValue(1000);*/
}
